import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/",
      redirect: "login",
    },
    {
      path: "/",
      name: "myMain",
      component: () => import("../view/myMain.vue"),
      children: [
        {
          path: "home",
          name: "myHome",
          component: () => import("../view/myHome.vue"),
        },
        {
          path: "member",
          name: "myMember",
          component: () => import("../view/myMember.vue"),
        },
        {
          path: "goods",
          name: "myGoods",
          component: () => import("../view/myGoods.vue"),
        },
        {
          path: "user",
          name: "myUser",
          component: () => import("../view/myUser.vue"),
        },
        {
          path: "order",
          name: "myOrder",
          component: () => import("../view/myOrder.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "myLogin",
      component: () => import("../view/myLogin.vue"),
    },
    {
      path: "*",
      name: "404",
      component: () => import("../view/404.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem("token");
  if (token) {
    next();
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next({ path: "/login" });
      Vue.prototype.$message({
        message: "请先登录!",
        type: "warning",
      });
    }
  }
  return;
});

export default router;
